<?php

namespace App\Logic;


use App\Models\Community;
use App\server\RankServer;
use App\server\RedisServer;
/**
 * 小区
 * @name zf
 */
class CommunityLogic{
     /**
     * Undocumented function
     * 列表
     * @param [type] $name
     * @param [type] $phone
     * @return void
     */
    public static function communityList($paginate){
        $redis=RedisServer::createRedisObject();
        $redis->select(10);
        $communityList= ['data'=>$redis->getAll()];
        if(empty($communityList)){
            if( empty($paginate)){
                $paginate=10;
            }
            $communityList=Community::list($paginate);
        }
       return $communityList;

    }
    /**
     * Undocumented function
     * 小区多条件搜索
     * @param [type] $name
     * @param [type] $phone
     * @return void
     */
    public static function communitySearch($name,$address,$paginate){
        if(!empty($name)){
            $where[]=['name','like',"%$name%"];
            RankServer::rank($name);
        }
        if(!empty($address)){
            $where[]=['loaction','like',"%$address%"];
        }

        if(empty($paginate)){
            $paginate=10;
        }
        $data = Community::wheres($where,$paginate);
        return $data;

    }
     /**
     * Undocumented function
     * 详情
     * @param [type] $id
     * @return void
     */
    public static function communityDetail($id){
        if(empty($id))throw new \Exception('请输入用户参数');
         $redis=RedisServer::createRedisObject();
         $redis->select(10);
         $community=$redis->hGetone($id);
         if(empty($community)){
             $community= Community::detail('id',$id);
             $data=$community->toArray();
             $redis->hSetone($data);
         }
         return $community;
    }
     /**
     * Undocumented function
     * 编辑信息
     * @param [type] $id
     * @return void
     */
    public static function communitySave($updata,$request){
        $request->validate([
            "name"=> "required",
            "loaction" => "required",
            "logo"=> "required",
            "text"=>  "required"
        ]);
       //判断用户是创建还是修改
       if(empty($updata['id'])||!isset($updata['id']) ){
            return Community::create($updata);
        }else{
            return Community::change('id',$updata['id'],$updata);
        }
    }
    /**
     * Undocumented function
     * 删除
     * @param [type] $id
     * @return void
     */
    public static function communityDel($vid){

        if(!empty($vid))throw new \Exception('请输入用户参数');
        return Community::del('id',$vid);
    }
     /**
     * 经济人信息存入redis
     */
    public static function communityRedis(){
        $community= Community::select('id','name','loaction','logo','clicks','text')->get()->toArray();
        $redis=RedisServer::createRedisObject();
        $redis->select(10);
        $redis->hSetAll($community);
    }
}

